/**
*      author: Y_Yao
*      created: 2025-02-12 19:40:58
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 6;
int n,m;


void solve()
{
    cin >> n >> m;
    vector<vector<int>> a(n + 1,vector<int>(m + 1,0));
    vector<array<int,2>> v(n + 1,{0,0});
    for(int i = 1;i <= n;i++)
    {
        int sum = 0;
        for(int j = 1;j <= m;j++)
        {
            cin >> a[i][j];
            v[i][0] += a[i][j];
            v[i][1] += a[i][j] * (m - j + 1); 
        }
    }
    int ans = 0;
    sort(v.begin() + 1,v.end(),[&](auto x,auto y)->bool{
        return x[0] > y[0];
    });
    for(int i = 1;i <= n;i++)
    {
        ans += v[i][1];
        ans += v[i][0] * m * (n - i);
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}